package event.collide;

import processing.core.PApplet;
import processing.core.PVector;
import run.Doodler;
import run.Env;
import run.LineCell;
import action.DirectionMutatingAction;

public class InflectionDirectionRule implements CollideRule {

	public void executeRule(LineCell c, LineCell collided) {
		
		Env e = Env.instance;
		final PApplet p = Doodler.instance;
		
		//rotate by t radians for time; a persistent influence
	    DirectionMutatingAction d = new DirectionMutatingAction() {
	    	final int time = (int) p.random(25,100);
	    	private int count = 0;
	    	float t = p.random(-0.05F, 0.05F);
	    	
			public void changeDir(PVector dir) {
				
				// Rotate by t rads
				if (isActive()) {
				    dir.x = dir.x * (float) Math.cos(t) - dir.y * (float) Math.sin(t);
				    dir.y = dir.x * (float) Math.sin(t) + dir.y * (float) Math.cos(t);
				    count++;
				}
			}
			
			public boolean isActive() {
				if (count < time)
					return true;
				else return false;
			}
	    };

	//TODO should we actually return this?
		// do this for 100 timesteps
	    
	    // what shoudl we do here?
	    // 		an OTHER cell is the target of the action...
		collided.addDirChanger(d);
	}

}
